﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace StateManager
{
    public static class Extension
    {
        /// <summary>
        /// hostedservice方式注入，运行时自动激活。
        /// 或手动AddSingleton方式注入，之后手动激活var manager = host.Services.GetService<SManager>();
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddStateManager(this IServiceCollection services)
        {
            return services.AddHostedService(sp =>
            {
                ILogger<SManager> logger = sp.GetService<ILogger<SManager>>();
                return new SManagerService((s, t, m) => logger?.LogInformation($"{s}\t{t}"), (s, e) => logger?.LogInformation($"事件类型\t{e}"));
            });
        }
    }


}
